/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset


* -------
* Table 2
* -------

* Globals
global dep "risk tax_rate red_i mixed_dec differentiated valitrade_advice"
global dep2 "ln_ival_usd ln_iwei nup_gap_i_int htrl_i_int e20"

* Matrix
mat table2_1 = J(4,6,.)
mat table2_1T = J(1,6,.)
mat table2_2 = J(4,5,.)
mat table2_2T = J(1,6,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' eis_f if sample_reg==1 , noab vce(cluster insp_f key_dec)
	
	mat table2_1[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table2_1[`i',`j'] = _se[eis_f]
	mat table2_1T[1,`j'] = e(N)
	local i = `i' + 2
	mat table2_1[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}

local i = 1
local j = 1
foreach x in $dep2 {
	
	qui: reghdfe `x' eis_f if sample_reg==1 , noab vce(cluster insp_f key_dec)
	
	mat table2_2[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat table2_2[`i',`j'] = _se[eis_f]
	mat table2_2T[1,`j'] = e(N)
	local i = `i' + 2
	mat table2_2[`i',`j'] = e(r2)
	local i = `i' -3
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table 2.xlsx", replace sheet(Table2) // create a new excel spreadsheet
putexcel B4 = matrix(table2_1), nformat(0.000) hcenter vcenter
putexcel B6 = matrix(table2_1T), nformat(number_sep) hcenter vcenter
putexcel C11 = matrix(table2_2), nformat(0.000) hcenter vcenter
putexcel C13 = matrix(table2_2T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:G1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* First part
putexcel A2 = "Dependent variable:", left vcenter bold
putexcel A4 = "Excess interaction share", left vcenter
putexcel A6 = "Observations", left vcenter
putexcel A7 = "R-squared", left vcenter
local cells `" "B" "C" "D" "E" "F" "G" "'
local labels `" "Risk score" "Tax rate" "Red channel dummy" "Mixed shipment dummy" "Differentiated share" "Valuation advice dummy" "' // group of labels
forvalues i = 1(1)6 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter
}

* Second part
putexcel A9 = "Dependent variable:", left vcenter bold
putexcel A11 = "Excess interaction share", left vcenter
putexcel A13 = "Observations", left vcenter
putexcel A14 = "R-squared", left vcenter
local cells `" "C" "D" "E" "F" "G" "'
local labels `" "Log initial value" "Log initial weight" "Log initial unit price (relative to internal prices)" "Initial hyp. tax rev. losses" "High potential tax yield dummy" "'
local j = 7
forvalues i = 1(1)5 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'9 = "`x'", hcenter vcenter bold
	putexcel `y'10 = "(`j')", hcenter vcenter
	local j = `j'+1
}


* -------------------------------- ENDS HERE -------------------------------- *